<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h1>jsonp</h1>
    <button id="btn">发送jsonp请求</button>
    <script>
        // function callback(data) {
        //     console.log("数据是", data);
        // }
        // const oBtn = document.querySelector("#btn")
        // let script = null
        // oBtn.onclick = function () {
        //     if (script) {
        //         script.remove()
        //     }
        //     script = document.createElement('script')
        //     script.src = "http://127.0.0.1:3000/getUser?userId=001&cb=callback"
        //     document.body.appendChild(script)
        // }

        // const obtn = document.getElementById("btn")
        // let script = null

        // function call(data) {
        //     console.log('数据是', data);
        // }
        // obtn.onclick = function () {

        //     if (script) {
        //         script.remove()
        //     }
        //     // 生成一个script元素地址是请求的服务器地址，查询字符串是函数的名字
        //     script = document.createElement("script")
        //     script.src = "http://127.0.0.1:3000/userinfo?userId=001&cb=call"
        //     document.body.appendChild(script)

        // }


        const obtn = document.querySelector("#btn")
        function callback(data) {
            console.log("数据是",data);
        }
        let script = null
        obtn.onclick = function () {
            if (script) {
                script.remove()
            }
            script = document.createElement("script")
            script.src = "http://127.0.0.1:3000/userinfo?userId=001&cb=callback"
            document.body.appendChild(script)
        }
    </script>


</body>

</html>